$(function(){
	var basePath=$("#basepath").val();
	//编辑按钮
	$(".college_edit").click(function(){
		var clgId=$(this).attr("data-collegeId");
		$("#college_edit_box_alert_container").html("");//清空提示框
		$.get(basePath+ "admin/college/getCollege/"+clgId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						$("#college_edit_box_form input[name='college_no']").val(data.no);
						$("#college_edit_box_form input[name='college_name']").val(data.name);
						$("#college_edit_box_form input[name='college_id']").val(data.id);
						$("#college_edit_box .ajax_wait").hide();
						$("#college_edit_box_form").show();
					}
		},"json");
	});
	//编辑->保存
	$("#college_edit_box_save").click(function(){
		var clgId =$("#college_edit_box_form input[name='college_id']").val();
		var clgNo =$("#college_edit_box_form input[name='college_no']").val();
		var clgName =$("#college_edit_box_form input[name='college_name']").val();
		$("#college_edit_box_alert_container .empty_field_error").alert('close');
		$("#college_edit_box_alert_container .repeat_no_error").alert('close');
		$.post(basePath+ "admin/college/updateCollege",{id:clgId,no:clgNo,name:clgName},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							makeAlert("字段不允许为空,请重新检查","error",$("#college_edit_box_alert_container"),"empty_field_error");
							break;
						case "-3":
							makeAlert("已存在编号为"+clgNo+"的学院,请检查后再输入","error",$("#college_edit_box_alert_container"),"repeat_no_error");
							break;
						case "-4":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"更新成功,正在刷新"});
							$("#college_edit_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	//删除按钮
	$(".college_delete").click(function(){
		$("#college_delete_box input[name='collegeId']").val($(this).attr("data-collegeId"));
	});
	//删除确认
	$("#college_delete_box_delete").click(function(){
		var clgId=$("#college_delete_box input[name='collegeId']").val();
		$.get(basePath+ "admin/college/deleteCollege/"+clgId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							noty({text:"删除错误:此系之下还有未删除的系",type:"error"});
							$("#college_delete_box").modal('hide');
							break;
						case "-3":
							noty({text:"删除错误:此系之下还有未删除的管理员",type:"error"});
							$("#college_delete_box").modal('hide');
							break;
						case "-4":
							noty({text:"删除错误:仅剩最后一个学院,不能删除",type:"error"});
							$("#college_delete_box").modal('hide');
							break;
						case "0":
							noty({text:"删除成功,正在刷新"});
							$("#college_delete_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	//添加按钮
	$("#college_add").click(function(){
		$("#college_add_box_alert_container").html("");
	});
	//添加确认
	$("#college_add_box_save").click(function(){
		var clgNo =$("#college_add_box_form input[name='college_no']").val();
		var clgName =$("#college_add_box_form input[name='college_name']").val();
		$("#college_add_box_alert_container .empty_field_error").alert('close');
		$("#college_add_box_alert_container .repeat_no_error").alert('close');
		$.post(basePath+ "admin/college/insertCollege",{no:clgNo,name:clgName},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							makeAlert("字段不允许为空,请重新检查","error",$("#college_add_box_alert_container"),"empty_field_error");
							break;
						case "-3":
							makeAlert("已存在编号为"+clgNo+"的学院,请检查后再输入","error",$("#college_add_box_alert_container"),"repeat_no_error");
							break;
						case "-4":
							alert("参数错误,请重试");
							history.go(0);
							break;
						case "0":
							noty({text:"添加成功,正在刷新"});
							$("#college_add_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
});